﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace BillingManagment
{
    public partial class AdvancePayment : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connection"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFarmerList();
                txtDate.Text = Convert.ToString(DateTime.Now.ToShortDateString());
            }
        }

        public void BindFarmerList()
        {
            SqlCommand cmd = new SqlCommand("select ID, (Name +' '+ village) as FName from tbl_Farmer", con);
            DataTable dt = new DataTable();
            SqlDataAdapter oAdp = new SqlDataAdapter(cmd);
            oAdp.Fill(dt);
            ddlFarmer.DataSource = dt;
            ddlFarmer.DataValueField = "ID";
            ddlFarmer.DataTextField = "FName";
            ddlFarmer.DataBind();
            ddlFarmer.Items.Insert(0, new ListItem("<----Select---->", "0"));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ddlFarmer.SelectedValue == "0" || txtAdvance.Text == "" || txtDate.Text == "")
            {
                Page.RegisterStartupScript("alert", "<script>alert('Fill Required Field');</script>");
            }
            else
            {
                try
                {
                    string AdvanceAmount = "";
                    SqlCommand cmd = new SqlCommand("select top 1 Advance_Balance from tbl_Saving_Ac where Farmer_ID='" + ddlFarmer.SelectedValue + "' Order By Modify_Date Desc", con);
                    DataTable dt = new DataTable();
                    SqlDataAdapter oAdp = new SqlDataAdapter(cmd);
                    oAdp.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        AdvanceAmount = dt.Rows[0][0].ToString();
                        AdvanceAmount = Convert.ToString(Convert.ToInt32(AdvanceAmount) + Convert.ToInt32(txtAdvance.Text));
                    }
                    else
                    {
                        AdvanceAmount = "0";
                        AdvanceAmount = Convert.ToString(Convert.ToInt32(AdvanceAmount) + Convert.ToInt32(txtAdvance.Text));
                    }


                    SqlCommand cmd1 = new SqlCommand("Insert into tbl_Saving_Ac(Farmer_ID,Modify_Date,Advance_Rs,Advance_Balance) values('" + ddlFarmer.SelectedValue + "',Convert(datetime,'" + txtDate.Text + "',103),'" + txtAdvance.Text + "','" + AdvanceAmount + "')", con);
                    DataTable dt1 = new DataTable();
                    SqlDataAdapter oAdp1 = new SqlDataAdapter(cmd1);
                    oAdp1.Fill(dt1);

                    ClearControl();
                    Page.RegisterStartupScript("alert", "<script>alert('Data Enter Successfully.');</script>");
                }
                catch
                {

                }
            }
        }


        public void ClearControl()
        {
            ddlFarmer.SelectedValue = "0";
            txtAdvance.Text = "";
            txtDate.Text = "";
        }
    }
}